package com.sql.common.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

public class PropertyUtil {

    private Properties properties;

    private String configFilePath = "config.properties";

    public PropertyUtil(String configFilePath){
        this.configFilePath = configFilePath;
        loadProperty();
    }

    public synchronized void setConfigFilePath(String path){
        configFilePath = path;
    }

    public synchronized void loadProperty(){
        FileInputStream fis=null;
        if(properties == null){
            properties = new Properties();
        }else{
            properties.clear();
        }
        try{
            fis = new FileInputStream(configFilePath);
            properties.load(fis);
        }catch (Exception e){
            try{
                properties.put("jdbc.driver.jarfile","ojdbc-1.7.jar");
                properties.put("jdbc.driver.user","cbs");
                properties.put("jdbc.driver.key","123");
                properties.put("jdbc.driver.url","jdbc:oracle:thin:@10.55.2.200:1521:cbstestdb");
                properties.put("jdbc.driver.driverClass","oracle.jdbc.OracleDriver");
                saveProperty();
            }catch (Exception e2){

            }
        }finally {
            try{
                fis.close();
            }catch (Exception e){

            }
        }
    }

    public synchronized void saveProperty(){
        FileOutputStream fos = null;
        try{
            fos = new FileOutputStream(configFilePath);
            properties.store(fos,"==========config==========");
        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            try{
                fos.close();
            }catch (Exception e){}
        }
    }

    public void putProperty(String key,String value){
        properties.put(key,value==null?"":value);
    }

    public String getProperty(String key){
        return properties.getProperty(key);
    }

}
